package com.huaao.micropolice.root.controllers.system;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.huaao.micropolice.common.util.JsonUtil;
import com.huaao.micropolice.core.common.page.GridPage;
import com.huaao.micropolice.core.common.page.Page;
import com.huaao.micropolice.core.model.system.User;
import com.huaao.micropolice.core.service.system.UserService;
import com.huaao.micropolice.root.controllers.BaseController;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

@Controller
@RequestMapping("system/user")
public class UserControllerSys extends BaseController {
	@Autowired
	private UserService userService;
	
	private static JsonConfig jsonConfig = new JsonConfig();
	static{
		jsonConfig.setExcludes(new String[]{"roles","dept","jobs","handler","hibernateLazyInitializer"});
	}
 
	@RequestMapping("index.do")
	public String index() {
		return "/system/user/index";
	}
	
	@RequestMapping(value="findUser.do",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String findUser(String name,GridPage gridPage) {
		Page page = userService.findByUserLike(name,gridPage);
		String result = JSONObject.fromObject(page,jsonConfig).toString();
		return result;
	}

	@RequestMapping("save.do")
	public ResponseEntity<String> save(User item) {
		try {
			if(StringUtils.isEmpty(item.getId())){
				userService.save(item);
			}else{
				userService.updateUser(item);
			}
			return super.ajaxModel(JSON, JsonUtil.toJson(successMap()));
		} catch (Exception e) {
			return super.ajaxModel(JSON, JsonUtil.toJson(failMap(e.getMessage())));
		}
	}
	
	@RequestMapping("del.do")
	public ResponseEntity<String> del(String id) {
		try {
			userService.del(id);
			return super.ajaxModel(JSON, JsonUtil.toJson(successMap()));
		} catch (Exception e) {
			return super.ajaxModel(JSON, JsonUtil.toJson(failMap(e.getMessage())));
		}
	}
	
	@RequestMapping("resetPassword.do")
	public ResponseEntity<String> resetPassword(@RequestParam(value = "ids[]")  String[] ids) {
		userService.resetPassword(ids);
		return super.ajaxModel(JSON, JsonUtil.toJson(successMap()));
	}
	
	@RequestMapping("dels.do")
	public ResponseEntity<String> dels(@RequestParam(value = "ids[]") String[] ids){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			for(String id: ids){
				userService.del(id);
			}
			map = successMap();
		} catch (Exception e) {
			map = failMap();
		}
		return super.ajaxModel(JSON, JsonUtil.toJson(map));
	}
	
	@RequestMapping("addUser.do")
	public ResponseEntity<String> addUser(User user){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			userService.addUser(user);
			map = successMap();
		} catch (Exception e) {
			map = failMap();
		}
		return super.ajaxModel(JSON, JsonUtil.toJson(map));
	}
	
	@RequestMapping("editPassword.do")
	public String editPassword(){
		return "system/user/passwd";
	}
	
	@RequestMapping("editUserInfo.do")
	public String editUserInfo(){
		return "system/user/info";
	}
}